package com.lcc.gulimall.ware.service.impl;

import com.lcc.common.to.SkuHasStockTo;
import com.lcc.common.utils.R;
import com.lcc.gulimall.ware.feign.ProductFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcc.common.utils.PageUtils;
import com.lcc.common.utils.Query;

import com.lcc.gulimall.ware.dao.WareSkuDao;
import com.lcc.gulimall.ware.entity.WareSkuEntity;
import com.lcc.gulimall.ware.service.WareSkuService;
import org.springframework.util.StringUtils;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
    @Autowired
    ProductFeignService productFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if(!StringUtils.isEmpty(skuId)){
            queryWrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if(!StringUtils.isEmpty(wareId)){
            queryWrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据采购项来添加库存
     * @param skuId 商品Id
     * @param wareId 仓库Id 添加到哪个仓库
     * @param skuNum 商品数量
     */
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //1.判断如果没有这个库存记录，则先进行新增操作
        List<WareSkuEntity> wareSkuEntities = this.list(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if(wareSkuEntities == null || wareSkuEntities.size() == 0){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            //远程查询sku名字，如果失败，整个事务不需要回滚
            //无需回滚方法1.catch异常
            //无需回滚方法2.TODO 还可以用什么办法让异常不会滚？高级篇
            try{
                R info = productFeignService.info(skuId);
                //查找成功
                if(info.getCode() == 0){
                    Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
                    wareSkuEntity.setSkuName((String) data.get("skuName"));
                }
            }catch (Exception e){

            }



            this.save(wareSkuEntity);
        }else {
            this.baseMapper.addStock(skuId, wareId, skuNum);
        }

    }

    /**
     * 批量查询商品是否有库存
     * @param skuIds
     * @return
     */
    @Override
    public List<SkuHasStockTo> getSkuHasStock(List<Long> skuIds) {
        List<SkuHasStockTo> collect = skuIds.stream().map(skuId -> {
            SkuHasStockTo to = new SkuHasStockTo();
            to.setSkuId(skuId);
            Long count = this.baseMapper.getSkuHasStock(skuId);
            to.setHasStock(count != null && count > 0);
            return to;
        }).collect(Collectors.toList());
        return collect;
    }

}